﻿#ifndef ANDYRP_LIT_INCLUDED
#define ANDYRP_LIT_INCLUDED

#include "Assets/Shaders/Util/Common.hlsl"
#include "Assets/Shaders/Util/Surface.hlsl"
#include "Assets/Shaders/Shadow/Shadow.hlsl"
#include "Assets/Shaders/Lighting/Light.hlsl"
#include "Assets/Shaders/Lighting/Lighting.hlsl"



TEXTURE2D(_baseMap);
SAMPLER(sampler_baseMap);

UNITY_INSTANCING_BUFFER_START(UnityPerMaterial)
	UNITY_DEFINE_INSTANCED_PROP(float4, _baseColor)
	UNITY_DEFINE_INSTANCED_PROP(float4, _baseMap_ST)
	UNITY_DEFINE_INSTANCED_PROP(float, _Cutoff)
UNITY_INSTANCING_BUFFER_END(UnityPerMaterial)

struct Attribute
{
	UNITY_VERTEX_INPUT_INSTANCE_ID
	float3 positionOS:POSITION;
	float2 baseUV:TEXCOORD0;
	float4 normal:NORMAL;
	
};

struct Varyings{
	UNITY_VERTEX_INPUT_INSTANCE_ID
	float4 posCS:SV_POSITION;
	float2 baseUV : VAR_BASE_UV;
	float3 normalWS:VAR_NORMAL;
	float3 posWS:TEXCOORD1;
};

Varyings LitPassVertex(Attribute input)
{
	Varyings output;
    float3 posWS =	TransformObjectToWorld(input.positionOS);
	output.posWS = posWS;
	output.posCS = TransformObjectToHClip(posWS);
	output.normalWS = TransformObjectToWorldNormal(input.normal.xyz);
	float4 baseST = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,_baseMap_ST);
	output.baseUV = input.baseUV * baseST.xy + baseST.zw;
	UNITY_SETUP_INSTANCE_ID(input);
	UNITY_TRANSFER_INSTANCE_ID(input, output);
    return output;
}


float4 LitPassFragment(Varyings input):SV_TARGET
{
	UNITY_SETUP_INSTANCE_ID(input);
	float4 baseMap = SAMPLE_TEXTURE2D(_baseMap, sampler_baseMap, input.baseUV);
	float4 baseColor = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _baseColor);
	float4 base = baseMap * baseColor;
	clip(base.a - UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,_Cutoff));
	Surface surface;
	
	surface.color = base.rgb;
	surface.alpha = base.a;
	surface.normal = normalize(input.normalWS);
	surface.worldPos = input.posWS;

	float3 color = GetLighting(surface);
	return base * float4(color, surface.alpha);
}
#endif
